﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PIM.API;

public partial class ChangePassWord : System.Web.UI.Page
{
    PIMService pimService = new PIMService();
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = "";
        if (!IsPostBack)
        {
            if (Request["u"] != null && Request["p"] != null)
            {
                try
                {
                    string userid = EncryptDecryptQueryString.Decrypt(HttpUtility.UrlDecode(Request["u"]));
                    string pass = EncryptDecryptQueryString.Decrypt(HttpUtility.UrlDecode(Request["p"]));
                    PIM.API.Users user = pimService.GetUserById(userid);
                    if (!pass.Equals(user.Password))
                        url = "index.aspx";
                    //ScriptManager.RegisterStartupScript(this, typeof(Page), "error", "window.onload = LoadUser('" + userid + "');changepass('" + userid + "');", true);
                }
                catch (Exception ex)
                {
                    url= "index.aspx";
                }
            }
            else
            {
                url = "index.aspx";
                
            }
            if (url.Length > 0)
            {
                Response.Redirect(url);
            }
        }
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        string url  = "";
        try
        {
            string userid = EncryptDecryptQueryString.Decrypt(HttpUtility.UrlDecode(Request["u"]));
            string pass = EncryptDecryptQueryString.Decrypt(HttpUtility.UrlDecode(Request["p"]));
            string result = pimService.ChangePassword(userid, confirm_password.Text);
            if (result == "NotOK" || result == "")
                ScriptManager.RegisterStartupScript(this, typeof(Page), "error", "alert('Can't change password')", true);
            else
            {
                //url = "index.aspx";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "error1", "window.onload = dgChangeSuccess;", true);
            }

        }
        catch (Exception ex)
        { 
        
        }
        
    }
}